package com.cn.pack.config;

import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@Component
@WebFilter(urlPatterns = "/*", asyncSupported = true)
public class HystrixRequestContextFilter implements Filter {

    public HystrixRequestContext context;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("初始化HystrixRequestContextFilter");
        context = HystrixRequestContext.initializeContext();
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        context.shutdown();
        System.out.println("关闭HystrixRequestContextFilter");
    }
}
